package com.example.game;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.File;

import static com.example.game.GamePage.databaseHelper;
import static com.example.game.GamePage.player;


public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
    String myTag = "myTag";
    String databaseTAG = "databaseTAG";
    private Intent intent_music;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bt1 = (Button)findViewById(R.id.bt1);
        Button bt2 = (Button)findViewById(R.id.bt2);
        Button bt3 = (Button)findViewById(R.id.bt3);
        Button bt4 = (Button)findViewById(R.id.bt4);
        TextView txtNormal = (TextView) findViewById(R.id.game_name);
        Typeface typeface = Typeface.createFromAsset(getAssets(), "MFYiCha_Noncommercial-Regular.ttf");

        bt2.setTypeface(typeface);
        bt3.setTypeface(typeface);
        bt1.setTypeface(typeface);
        bt4.setTypeface(typeface);
        txtNormal.setTypeface(typeface);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        setDataPath();
        // 启动服务播放背景音乐
        intent_music = new Intent(MainActivity.this, MyIntentService.class);
        String action = MyIntentService.ACTION_MUSIC;
        // 设置action
        intent_music.setAction(action);
        startService(intent_music);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (intent_music != null){
            // 对于intentService，这一步可能是多余的
            stopService(intent_music);
        }
    }
    //实现监听器，处理响应
    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.bt1:
                //暂时用作删除存档键
                //点击了“新游戏”

                Log.d(databaseTAG, "onCreateDatabase: checkpoint 2");
                String DATA_PATH = getFilesDir().getPath() + "data/"+getPackageName()+"/databases";
                Log.d(databaseTAG, DATA_PATH);
                File dbFile = new File(DATA_PATH);
                boolean t = dbFile.mkdirs();
                if(t){
                    Log.d(databaseTAG, "mkdir successed");
                }else{
                    Log.d(databaseTAG, "mkdir failed");
                }
                Log.d(databaseTAG, "onCreateDatabase: checkpoint 3");
                final SQLiteDatabase db=SQLiteDatabase.openOrCreateDatabase(DATA_PATH + "/PlayerInfo.db",null);
                Log.d(databaseTAG, "onCreateDatabase: checkpoint 4 : create database finished");
                //完成实例化SQLiteDatabase
//                SQLiteDatabase db = databaseHelper.getDataBase();
                if(databaseHelper.check_saved_file()) {
                    String sql_cmd = "DROP TABLE user";
                    db.execSQL(sql_cmd);
                    Log.d(databaseTAG, "    RECORD     DELATED ");
                }
                Log.d(databaseTAG, "record not existed, creating new record");      //表中没有记录，新建一条记录（开始新游戏）
                databaseHelper.make_save_file();
                String sql_cmd = "insert into user values(?";
                for (int i = 0; i < player.BUILDING_TYPE_COUNT; i++) {
                    sql_cmd = sql_cmd + ",?";
                }
                sql_cmd = sql_cmd + ")";
                Log.d(databaseTAG, "Creating new record : SQL_CMD = " + sql_cmd);
                int[] empty_array_list = new int[21];    //新存档为全0
                player.new_record();
                db.execSQL(sql_cmd, new Object[]{empty_array_list});

                Log.d(myTag, "一啊");
                //点击了开始游戏
                Intent  intent2 = new Intent();
                intent2.setClass(this,GamePage.class);
                intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent2);
                Log.d(myTag, "三啊");
                break;
            case  R.id.bt2:
                Log.d(myTag, "一啊");
                //点击了“继续游戏”
                Intent  intent1 = new Intent();
                intent1.setClass(this,GamePage.class);
                intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent1);
                Log.d(myTag, "三啊");
                break;
            case  R.id.bt3:
                Log.d("intentTAG", "button:turn to page 3");
                //点击了新手教程
                Intent  intent3 = new Intent();
                intent3.setClass(this,HelpPage.class);
                intent3.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent3);
                break;
            case  R.id.bt4:
                Log.d("intentTAG", "button:turn to page 4");
                //点击了关于

                Intent  intent4 = new Intent();
                intent4.setClass(this,About_Page.class);
                intent4.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent4);
                break;
        }
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //点击返回键调用方法
        if(keyCode== KeyEvent.KEYCODE_BACK){
            finish();
        }
        return false;
    }
    public void setDataPath() {
        player.DATA_PATH = getFilesDir().getPath() + "data/" + getPackageName() + "/databases";
    }
}
